package com.light.roller;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.modifier.IModifier;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;

import com.heyzap.sdk.HeyzapLib;
import com.huw.cameramanagers.BlackStartEndActivity;
import com.huw.debuger.Debugger;
import com.huw.debuger.FileIO;
import com.huw.debuger.FileIO.FileName;
import com.light.music.BackGroundMusicController;
import com.light.roller.Roller.RollerState;


public class Launcher extends BlackStartEndActivity {


	public Menu menu;
	public Sprite m_logoSprite;
	public Sprite m_andEngineSprite;
	public Sprite m_blackSprite;
	public Sprite m_forFacebookSprite;
	
    @Override
    protected void onResume() {
        super.onResume();
        
        BackGroundMusicController.changeMusic(MediaPlayer.create(this, com.light.roller.R.raw.cosmic));
        if (FileIO.LoadGame(this, FileName.Volume) >= 1)
        {
        	BackGroundMusicController.setVolume(1);
        	Roller.playMusic = true;
        }
        else
        {
        	BackGroundMusicController.setVolume(0);
        	Roller.playMusic = false;
        }
        BackGroundMusicController.Play();
    }

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		HeyzapLib.load(this);
	}
	
	@Override
	public Engine onLoadEngine() 
	{
		Engine temp = super.onLoadEngine();
		
		menu = new Menu(this);
		menu.onLoadEngine();
		
		//BackGroundMusicController.Stop();
		return temp;
	}

	@Override
	public void onLoadResources() {
		
		super.onLoadResources();
		menu.onLoadResources();

		ResourceManager.onLoadResources("BDSLogo", this);
		ResourceManager.onLoadResources("Black", this);
		ResourceManager.onLoadResources("AndLogo", this);
		
	}

	@Override
	public Scene onLoadScene() {
		super.onLoadScene();
		menu.onLoadScene(scene, physicsWorld);
		
		m_forFacebookSprite = new Sprite(75, 360, ResourceManager.getTextureRegion("Black")){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/pages/Constant-C/151551618273259"));
				BackGroundMusicController.Stop();
				startActivity(browserIntent);
				return true;
			}
		};
		m_forFacebookSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_forFacebookSprite.setAlpha(0.0f);
		m_forFacebookSprite.setSize(168, 63);
		scene.attachChild(m_forFacebookSprite);
		scene.registerTouchArea(m_forFacebookSprite);
		
		m_logoSprite = new Sprite(0, 0, ResourceManager.getTextureRegion("BDSLogo"));
		m_logoSprite.setPosition(CAMERA_WIDTH / 2 - m_logoSprite.getWidth() / 2,
						         CAMERA_HEIGHT / 2 - m_logoSprite.getHeight() / 2);
		m_logoSprite.setScale(0.35f);
		m_blackSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_WIDTH, ResourceManager.getTextureRegion("Black"));
		m_andEngineSprite = new Sprite(0, 0, ResourceManager.getTextureRegion("AndLogo"));
		m_andEngineSprite.setScale(2);
		m_andEngineSprite.setPosition(CAMERA_WIDTH / 2 - m_andEngineSprite.getWidth() / 2,
		         CAMERA_HEIGHT / 2 - m_andEngineSprite.getHeight() / 2);
		
		
		m_andEngineSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_logoSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_blackSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_andEngineSprite.setAlpha(0.0f);
		scene.attachChild(m_blackSprite);
		scene.attachChild(m_logoSprite);
		scene.attachChild(m_andEngineSprite);
		final IEntityModifierListener andEngineModifilerListener = new IEntityModifierListener() {

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				SequenceEntityModifier blackModifiler = new SequenceEntityModifier(
						new AlphaModifier(0.5f, 1, 1),
						new AlphaModifier(1.0f, 1, 0));
				m_blackSprite.registerEntityModifier(blackModifiler);	
			}
		};
		
		/*
		IEntityModifierListener BDSlogoModifilerListener = new IEntityModifierListener() {

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				SequenceEntityModifier aneEngineModifiler = new SequenceEntityModifier(
						andEngineModifilerListener,
						new AlphaModifier(1.0f, 0, 0),
						new AlphaModifier(0.5f, 0, 1),
						new AlphaModifier(3.0f, 1, 1),
						new AlphaModifier(0.5f, 1, 0));
				m_andEngineSprite.registerEntityModifier(aneEngineModifiler);	
			}
		};
		*/
		
		ParallelEntityModifier BDSlogoModifiler = new ParallelEntityModifier(
				andEngineModifilerListener,
				new SequenceEntityModifier(new AlphaModifier(1.5f, 0, 1), 
										   new AlphaModifier(3.5f, 1, 1), 
										   new AlphaModifier(1.5f, 1, 0)),
				new ScaleModifier(7.5f, 0.33f, 0.42f));

		
		m_logoSprite.registerEntityModifier(BDSlogoModifiler);
		BlackSceneCover();
		return scene;
	}
	



	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}	



	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if (keyCode == KeyEvent.KEYCODE_BACK ||
	    	 keyCode == KeyEvent.KEYCODE_HOME)
	    	 BackGroundMusicController.Stop();

	     return super.onKeyDown(keyCode, event);    
	}


}
